<template>
	<view>
		<image class="bgImage" src="/static/image/guide/start.png" mode="widthFix"></image>
		<u-popup :show="showpopup" mode="center" :round="10">
			<view class="popup">
				<image @click="closeUpdate" class="colse" src="https://cdn.yic3.cn/lawyer/mp-xhs/guide/closeWhite.png"
					mode="">
				</image>
				<image src="https://cdn.yic3.cn/lawyer/mp-xhs/guide/updateBg.png" v-if="" mode="scaleToFill"></image>
				<view class="text">1.新版本上线 </view>
				<view class="text1">2.UI界面优化升级；</view>
				<view class="text2">3.修复了已知bug，使用更流畅；</view>
				<view class="buttom" @click="loaddown()">立即下载</view>
				<view class="text3">发现新版本</view>
				<view class="text4">v{{versionInfo.version}}</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	// #ifdef APP
	const pluginImpliOS = uni.requireNativePlugin('YCBDAPlugin-YCBADModule')
	const adModule = uni.requireNativePlugin('AppleAdServicesPlugin-AdServicesPluginModule')
	// #endif
	export default {
		data() {
			return {
				versionInfo: {},
				showpopup: false,
				idfv: '',
				idfa: '',
			};
		},
		onLoad(e) {
			let that = this
			uni.onNetworkStatusChange(function(res) {
				if (res.isConnected && !uni.getStorageSync('config')) {
					that.getSysInfo()
				}
			});
			// #ifdef MP-WEIXIN
			if (e['click_id']) {
				uni.setStorageSync('click_id', e['click_id'])
			}
			wx.reportEvent("guide_into", {})
			// #endif

			// #ifdef APP
			this.idfv = pluginImpliOS?.getDeviceIDFV()
			plus.statistic.eventTrig('app_start')
			this.$api.userEvent({
				type: 'click',
				key: 'app_start',
				value: "app_start",
				extra: '打开app'
			}).then(res => {})
			// #endif
			this.getSysInfo()
		},
		methods: {
			getSysInfo() {
				let that = this
				uni.getSystemInfo({
					async success(res) {
						uni.setStorageSync('saveAllList', res)
						// #ifdef APP
						plus.device.getInfo({
							success(e) {
								console.log(e);
								that.idfa = e.idfa
								uni.setStorageSync('idfa', e.idfa)
								that.getLanch()
							},
							fail(err) {
								that.getLanch()
							}
						})
						// #endif
						// #ifdef MP
						that.getLanch()
						// #endif
					},
					fail() {
						that.getLanch()
					}
				})
			},
			async getLanch() {
				await this.launch()
				// #ifdef MP-WEIXIN
				if (!uni.getStorageSync('sessionKey')) {
					this.getSession()
				}
				this.nextTo()
				// #endif

				// #ifdef APP || APP-PLUS
				this.oceanFun()
				this.checkUpdate()
				// #endif
			},
			/* 检查更新 */
			checkUpdate() {
				let that = this
				this.versionInfo = uni.getStorageSync('config') && uni.getStorageSync('config')[
					'client.version.upgrade'].ios
				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					// 检测版本更新
					if ((widgetInfo.version.split('.').join('') - 0) < (that.versionInfo.version.split('.').join(
							'') - 0)) {
						that.showpopup = true
					} else {
						that.nextTo()
					}
				})
			},
			nextTo() {
				// if (!uni.getStorageSync('guideIsOver') && uni.getStorageSync('config')['client.guide.enable']) {
				// 	uni.reLaunch({
				// 		url: '/guide/pages/guide_2/guide_2'
				// 		// url: '/pages/index/home/home'
				// 	})
				// } else {
				// 	uni.reLaunch({
				// 		url: '/pages/index/home/home'
				// 	})
				// }
				uni.reLaunch({
					url: '/upload/pages/chooseFiles/chooseFiles'
					// url: '/pages/index/home/home'
				})
			},
			// 巨量归因
			async oceanFun() {
				let that = this
				try {
					if (plus.os.name == 'iOS') {
						pluginImpliOS.registerBda()
						that.getMatch()
					}
				} catch (e) {
					console.log(e)
				}


			},
			getMatch() {
				let data = {
					oaid: '',
					idfa: this.idfa,
					imei: '',
					idfv: plus.os.name == 'iOS' ? this.idfv : '',
					ua: '',
					androidId: ''
				}
				console.log(data)
				this.$api.match(data).then(res => {
					console.log(res)
				})
			},
			// 是否有sess
			getSession() {
				let that = this
				uni.login({
					complete(res) {
						that.$api.getSession({
							code: res.code
						}).then(res => {
							console.log(res);
							if (res.data.code == 0) {
								uni.setStorageSync('sessionKey', res.data.data)
								uni.getUserInfo({
									complete(res) {
										console.log(res);
										that.$api.phonelogin({
											loginType: 'wxapplet',
											wxapplet: {
												"iv": res.iv,
												"sessionKey": uni
													.getStorageSync(
														'sessionKey'),
												"encryptedData": res
													.encryptedData
											}
										}).then(e => {
											if (e.data.code == 0) {
												uni.setStorageSync('Token',
													e.data.data.token)
											}
										})
									}
								})
							}
						})
					}
				})
			},
			// 关闭更新弹窗
			closeUpdate() {
				this.showpopup = false
				if (this.versionInfo.force) {
					return plus.runtime.quit();
				}
				this.goNext()

			},
			loaddown() {
				let _this = this;
				this.showpopup = false
				let appleId = '6478996388'
				return plus.runtime.launchApplication({
					// action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
					action: _this.versionInfo.url
				}, function(e) {
					console.log('Open system default browser failed: ' + e.message);
				});

			},
		}
	}
</script>

<style lang="scss" scoped>
	.bgImage {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		width: 100%;
		display: block;
		height: 100vh;
	}

	.popup {
		width: 600rpx;
		height: 841.49rpx;
		background-color: transparent;
		position: relative;

		.text3 {
			width: 240rpx;
			height: 72rpx;
			position: absolute;
			left: 44rpx;
			top: 158rpx;
			z-index: 111111;
			font-size: 48rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 56rpx;
		}

		.text4 {
			width: 200rpx;
			height: 72rpx;
			position: absolute;
			left: 44rpx;
			top: 226rpx;
			z-index: 111111;
			font-size: 40rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 47rpx;
		}

		.text5 {
			width: 484rpx;
			height: 38rpx;
			position: absolute;
			left: 58rpx;
			top: 474rpx;

			.text6 {
				position: absolute;
				left: 424rpx;
				top: -40rpx;
				font-size: 32rpx;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 500;
				color: #1A89FF;
				line-height: 40rpx;
				z-index: 111111;
			}
		}

		.text7 {
			font-size: 28rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 500;
			color: rgba(51, 51, 51, 0.7);
			line-height: 40rpx;
			position: absolute;
			left: 188rpx;
			top: 542rpx;
			z-index: 111111;
		}

		.text8 {
			position: absolute;
			top: 698rpx;
			left: 66rpx;
			width: 468rpx;
			height: 84rpx;
			background: linear-gradient(102deg, #3C8DEE 0%, #3F63DB 100%);
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 40rpx;
			font-size: 32rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 40rpx;
			opacity: 0.5;
		}

		.colse {
			position: absolute;
			top: 144rpx;
			left: 518rpx;
			width: 40rpx;
			height: 40rpx;
			z-index: 111111;
			opacity: 1;
		}

		.colse:active {
			opacity: 0.8;
		}

		>image:nth-child(2) {
			width: 100%;
			height: 100%;
			font-size: 30rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: rgba(51, 51, 51, 0.7);
			line-height: 35rpx;
		}

		.text {
			position: absolute;
			left: 48rpx;
			top: 422rpx;
			font-size: 30rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: rgba(51, 51, 51, 0.7);
			line-height: 35rpx;
		}

		.text1 {
			position: absolute;
			left: 48rpx;
			top: 482rpx;
			font-size: 30rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: rgba(51, 51, 51, 0.7);
			line-height: 35rpx;
		}

		.text2 {
			position: absolute;
			left: 48rpx;
			top: 542rpx;
			font-size: 30rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: rgba(51, 51, 51, 0.7);
			line-height: 35rpx;
		}

		.buttom {
			position: absolute;
			top: 698rpx;
			left: 66rpx;
			width: 468rpx;
			height: 84rpx;
			background: linear-gradient(102deg, #3C8DEE 0%, #3F63DB 100%);
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 40rpx;
			font-size: 32rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 40rpx;
			opacity: 1;
		}

		.buttom:active {
			opacity: 0.8;
		}
	}

	/deep/.u-popup__content {
		background-color: transparent !important;
	}
</style>